//
//  SecondViewController.m
//  UI_元旦豆瓣半完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "SecondViewController.h"
#import "UIImageView+WebCache.h"
#import <sqlite3.h>
#import "MyDataBaseHandle.h"
#import "ActivityModel.h"
#import "ActivityCllectViewController.h"


#define WIDCH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SecondViewController ()


@end

@implementation SecondViewController

- (void)dealloc {
    [_detail release];
    [_secDic release];
    [_mArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = _secDic[@"title"];
    UIImage *shouImage = [UIImage imageNamed:@"iconfont-shoucang-1.png"];
    shouImage = [shouImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:shouImage style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    UIImage *backImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(item2Action:)];
    self.navigationItem.leftBarButtonItem = item2;

    
//    NSLog(@"%@", _secDic);
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDCH, HEIGHT)];
    [self.view addSubview:scroll];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, WIDCH - 40, 50)];
    title.font = [UIFont systemFontOfSize:25];
    title.backgroundColor = [UIColor clearColor];
    [scroll addSubview:title];
    title.text = _secDic[@"title"];
    [title release];
    
    UIImageView *myImageView = [[UIImageView alloc] init];
    myImageView.frame = CGRectMake(20, 80, 100, 150);
    [scroll addSubview:myImageView];
    NSString *imageStr = _secDic[@"image"];
    [myImageView sd_setImageWithURL:[NSURL URLWithString:imageStr]];
    [myImageView release];
    
    UIImageView *timeImage = [[UIImageView alloc] initWithFrame:CGRectMake(125, 80, 20, 20)];
    timeImage.image = [UIImage imageNamed:@"icon_date_blue@2x.png"];
    [scroll addSubview:timeImage];
    [timeImage release];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 75, 220, 30)];
    NSString *begin = [_secDic[@"begin_time"] substringWithRange:NSMakeRange(5, 11)];
    NSString *end = [_secDic[@"end_time"] substringWithRange:NSMakeRange(5, 11)];
    timeLabel.text = [NSString stringWithFormat:@"%@ -- %@", begin, end];
    timeLabel.font = [UIFont systemFontOfSize:15];
    [scroll addSubview:timeLabel];
    [timeLabel release];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(125, 110, 20, 20)];
    image.image = [UIImage imageNamed:@"icon_sponsor_blue@2x.png"];
    [scroll addSubview:image];
    [image release];
    
    UILabel *selfLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 105, 220, 30)];
    selfLabel.text = _secDic[@"owner"][@"name"];
    selfLabel.font = [UIFont systemFontOfSize:15];
    [scroll addSubview:selfLabel];
    [selfLabel release];
    
    UIImageView *caImage = [[UIImageView alloc] initWithFrame:CGRectMake(125, 140, 20, 20)];
    caImage.image = [UIImage imageNamed:@"icon_catalog_blue@2x.png"];
    [scroll addSubview:caImage];
    [caImage release];
    
    UILabel *caLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 135, 220, 30)];
    caLabel.text = [NSString stringWithFormat:@"%@%@", @"类型: ", _secDic[@"category_name"]];
    caLabel.font = [UIFont systemFontOfSize:15];
    [scroll addSubview:caLabel];
    [caLabel release];
    
    UIImageView *addressImage = [[UIImageView alloc] initWithFrame:CGRectMake(125, 170, 20, 20)];
    addressImage.image = [UIImage imageNamed:@"icon_spot_blue@2x.png"];
    [scroll addSubview:addressImage];
    [addressImage release];
    
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 160, 220, 60)];
    addressLabel.font = [UIFont systemFontOfSize:15];
    addressLabel.text = _secDic[@"address"];
    [scroll addSubview:addressLabel];
    [addressLabel release];
    addressLabel.numberOfLines = 10;
    
    UILabel *actiLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 100, 50)];
    actiLable.text = @"活动介绍";
    actiLable.font = [UIFont boldSystemFontOfSize:25];
    [scroll addSubview:actiLable];
    [actiLable release];
    
    // 通过文字大小获取文本的高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    // 通过文字大小获取文本的高度
    CGRect rect = [_secDic[@"content"] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    UILabel *detailLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, WIDCH - 40, rect.size.height)];
    detailLable.text = _secDic[@"content"];
    detailLable.font = [UIFont systemFontOfSize:15];
    [scroll addSubview:detailLable];
    [detailLable release];
    detailLable.numberOfLines = 100;
    
    
    // scroll.contentSize自适应
    scroll.contentSize = CGSizeMake(0, rect.size.height + 400);
    
    // 收藏成功时  星星保持红色
    NSArray *arr = [[MyDataBaseHandle sharedDataBaseHandle] selectTable];
    if ([arr containsObject:_secDic[@"title"]]) {
    UIImage *shouImage = [UIImage imageNamed:@"iconfont-shoucang-2.png"];
    shouImage = [shouImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:shouImage style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    }
}

- (void)item2Action:(UIBarButtonItem *)item {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)itemAction:(UIBarButtonItem *)item {
    
    NSArray *arr = [[MyDataBaseHandle sharedDataBaseHandle] selectTable];
//    NSLog(@"😈😈😈😈😈%@", arr);
        if (![arr containsObject:_secDic[@"title"]]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"收藏成功😎" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
        
//        NSLog(@"%@", _mArr);
        // 点击收藏时 星星变红
        UIImage *shouImage = [UIImage imageNamed:@"iconfont-shoucang-2.png"];
        shouImage = [shouImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:shouImage style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
        self.navigationItem.rightBarButtonItem = item;
            
            ActivityModel *model = [ActivityModel activityModel];
            model.title = _secDic[@"title"];
            [[MyDataBaseHandle sharedDataBaseHandle] insertModel:model];
//            self.arr = [[MyDataBaseHandle sharedDataBaseHandle] selectTable];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已收藏过了！👻" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
  }
    





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
